
//
//  HMMainTabBar.swift
//  Weibo
//
//  Created by 米易雅克 on 16/4/9.
//  Copyright © 2016年 米易雅克. All rights reserved.
//

import UIKit

class HMMainTabBar: UITabBar {
    
    var composeClosure : (() -> ())?
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let buttonCount = 5
        
        let width = self.frame.size.width / CGFloat(buttonCount)
        
        let newFrame = CGRect(x: 0, y: 0, width: width, height: frame.size.height)
        
        var index = 0
        for subView in subviews {
            
            if subView.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                subView.frame = CGRectOffset(newFrame, CGFloat(index)*width, 0)
                
                index += index == 1 ? 2 : 1
                
            }
            
        }
        
    composeButton.frame = CGRect(x: 2*width, y: 0, width: width, height: frame.size.height)
        
        
    }
    
    @objc private func didClickComposeButton()  {
    
    composeClosure?()
    }
    
    
    
    private lazy var composeButton : UIButton = {
    
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named:"tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), forState: UIControlState.Selected)
        button.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setImage(UIImage(named:"tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.addTarget(self, action: #selector(HMMainTabBar.didClickComposeButton), forControlEvents: UIControlEvents.TouchUpInside)
        
        
        
        self.addSubview(button)
        return button
        
    
    }()
    
    
    

}
